#include "MasterPanelForm.h"
#include "ui_MasterPanelForm.h"
#include <QTimer>
#include "common/Utils.h"

MasterPanelForm::MasterPanelForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MasterPanelForm)
{
    ui->setupUi(this);

    m_Thread = new QThread();
    m_ModBusTcpWorker = new ModBusTcpWorker(nullptr);

    // 将 worker 移动到新线程，并确保在线程结束时删除
    m_ModBusTcpWorker->moveToThread(m_Thread);
    connect(m_Thread, &QThread::finished, m_Thread, &QObject::deleteLater);

    // 当 worker 被删除时，结束线程
    connect(m_ModBusTcpWorker, &QObject::destroyed, m_Thread, &QThread::quit);

    // 获取QCoreApplication的实例
    QCoreApplication *app = QCoreApplication::instance();
    if(app)
    {
        // 确保在应用程序退出时线程已经结束
        connect(app, &QCoreApplication::aboutToQuit, m_Thread, &QThread::quit);
        connect(m_Thread, &QThread::finished, app, &QCoreApplication::quit);
    }

    connect(this,&MasterPanelForm::sig_SetProNameDevType,m_ModBusTcpWorker,&ModBusTcpWorker::slotSetProNameDevType,Qt::QueuedConnection);
    connect(this,&MasterPanelForm::sig_SetIPPort,m_ModBusTcpWorker,&ModBusTcpWorker::slotSetIPPort,Qt::QueuedConnection);
    connect(this,&MasterPanelForm::sig_SetReadStartAddressAndRegisterCount,m_ModBusTcpWorker,&ModBusTcpWorker::slotSetReadStartAddressAndRegisterCount,Qt::QueuedConnection);
    connect(this,&MasterPanelForm::sig_StartLoopReadRegister,m_ModBusTcpWorker,&ModBusTcpWorker::slotStartConnection,Qt::QueuedConnection);
    connect(this,&MasterPanelForm::sig_WriteRegister,m_ModBusTcpWorker,&ModBusTcpWorker::slotAddWriteQueue,Qt::QueuedConnection);


    connect(m_ModBusTcpWorker,&ModBusTcpWorker::sig_ConnectionState,this,&MasterPanelForm::slot_ConnectionState,Qt::QueuedConnection);
    connect(m_ModBusTcpWorker,&ModBusTcpWorker::sig_readfloatData,this,&MasterPanelForm::slot_ReadfloatData,Qt::QueuedConnection);
    connect(m_ModBusTcpWorker,&ModBusTcpWorker::sig_readBoolData,this,&MasterPanelForm::slot_ReadBoolData,Qt::QueuedConnection);

//    QTimer::singleShot(1000, [&]() {

//    });
    m_Thread->start();


}

MasterPanelForm::~MasterPanelForm()
{
    delete ui;
}

void MasterPanelForm::setProNameDevType(QString proName, int devType)
{
    emit sig_SetProNameDevType(proName,devType);
}

void MasterPanelForm::setIPPort(QString address, short port)
{
    ui->MasterIP->setText(address);
    emit sig_SetIPPort(address,port);
}

void MasterPanelForm::setReadStartAddressAndRegisterCount(QList<int> startAddress, QList<int> registerCount, int RegisterType)
{
    emit sig_SetReadStartAddressAndRegisterCount(startAddress,registerCount,RegisterType);
}

void MasterPanelForm::startLoopReadRegister(bool isStart)
{
    emit sig_StartLoopReadRegister(isStart);
}

void MasterPanelForm::slot_ConnectionState(bool State)
{
    if(State)
    {
       ui->MasterIP->setStyleSheet("color:#4db5f6");
    }
    else
    {
       ui->MasterIP->setStyleSheet("color:#808080");
       ui->labValue_1->setText("--");
       ui->labValue_2->setText("--");
       ui->labValue_3->setText("--");

       ui->btnState_1->setChecked(false);
       ui->btnState_2->setChecked(false);
       ui->btnState_3->setChecked(false);
       ui->btnState_4->setChecked(false);
       ui->btnState_5->setChecked(false);
       ui->btnState_6->setChecked(false);

       ui->btnLowFre->setStyleSheet("background-color: #808080;");
       ui->btnHighFre->setStyleSheet("background-color: #808080;");

       emit sig_Dev1fault(false);
       emit sig_Dev2fault(false);
       emit sig_Dev3fault(false);
       emit sig_Dev4fault(false);
       emit sig_Dev5fault(false);
       emit sig_Dev6fault(false);
       emit sig_Dev7fault(false);
       emit sig_Dev8fault(false);
    }
}

void MasterPanelForm::slot_ReadfloatData(int MsgId,QList<float> data)
{
    Q_UNUSED(MsgId)//总控消息唯一 不需要id
    if(data.size() != 3)
        return;
    ui->labValue_1->setText(QString::number(data.at(0),'f',1));
    ui->labValue_2->setText(QString::number(data.at(1),'f',1));
    ui->labValue_3->setText(QString::number(data.at(2),'f',1));
}

void MasterPanelForm::slot_ReadBoolData(int MsgId,QList<bool> data)
{
    Q_UNUSED(MsgId)//总控消息唯一 不需要id
    if(data.size() != 16)
        return;

    if(data.at(1))
    {
        ui->btnHighFre->setStyleSheet("background-color: #4caf50;");
        ui->btnLowFre->setStyleSheet("background-color: #808080;");
    }
    else
    {
        ui->btnHighFre->setStyleSheet("background-color: #808080;");
        ui->btnLowFre->setStyleSheet("background-color: #4caf50;");
    }

    //可读写的先记录第一个modbus点位
    readData1 = 0;
    readData1 = quint16ModifyBit(readData1,0,data.at(0));
    readData1 = quint16ModifyBit(readData1,1,data.at(1));


    QList<bool>  faultBool;
    for(int i = 2; i<= 9; i++)
    {
        faultBool << data.at(i);
    }

    //取反 LX
    if(faultBool.size() == 8)
    {
        emit sig_Dev1fault(!faultBool.at(0));
        emit sig_Dev2fault(!faultBool.at(1));
        emit sig_Dev3fault(!faultBool.at(2));
        emit sig_Dev4fault(!faultBool.at(3));
        emit sig_Dev5fault(!faultBool.at(4));
        emit sig_Dev6fault(!faultBool.at(5));
        emit sig_Dev7fault(!faultBool.at(6));
        emit sig_Dev8fault(!faultBool.at(7));
    }


    ui->btnState_1->setChecked(!data.at(10));
    ui->btnState_2->setChecked(!data.at(11));
    ui->btnState_3->setChecked(!data.at(12));
    ui->btnState_4->setChecked(!data.at(13));
    ui->btnState_5->setChecked(!data.at(14));
    ui->btnState_6->setChecked(!data.at(15));
}

void MasterPanelForm::on_btnHighFre_clicked()
{
    quint16  iValue =  quint16ModifyBit(readData1,1,true);
    QList<quint16> sendData1;
    sendData1 << iValue;

    WriteData* writeData1 = new WriteData(2,sendData1.size(),sendData1);
    emit sig_WriteRegister(writeData1);
}

void MasterPanelForm::on_btnLowFre_clicked()
{
    quint16  iValue = quint16ModifyBit(readData1,1,false);
    QList<quint16> sendData1;
    sendData1 << iValue;
    WriteData* writeData1 = new WriteData(2,sendData1.size(),sendData1);
    emit sig_WriteRegister(writeData1);
}

void MasterPanelForm::on_btnReset_clicked()
{
    quint16  iValue = quint16ModifyBit(readData1,0,true);
    QList<quint16> sendData1;
    sendData1 << iValue;
    WriteData* writeData1 = new WriteData(2,sendData1.size(),sendData1);
    emit sig_WriteRegister(writeData1);
}
